<template>
    <div class="mytext-layout">
        <div class="mytext-nav">
            <div class="nav-container">
                <div class="left-items">
                    <template v-for="(nav, index) in mynavs.filter(item => item.type === 'selection')" :key="index">
                        <div class="nav-item">
                            <span class="nav-label">{{ nav.name }}：</span>
                            <select class="nav-select" v-model="selectedValues[nav.column]">
                                <option 
                                    v-for="option in nav.options" 
                                    :key="option.value" 
                                    :value="option.value"
                                    class="select-option"
                                >
                                    {{ option.label }}
                                </option>
                            </select>
                        </div>
                    </template>
                </div>
                <div class="right-items">
                    <template v-for="(nav, index) in mynavs.filter(item => item.type === 'input')" :key="index">
                        <div class="nav-item">
                            <div class="search-container">
                                <input
                                    type="text"
                                    v-model="searchText"
                                    :placeholder="nav.placeholder"
                                    class="nav-input"
                                >
                                <span class="search-icon">
                                  <Icon type="ios-search" />
                                </span>
                            </div>
                        </div>
                    </template>
                </div>
            </div>
        </div>
        <LIST v-model:ListData="textdata" :FuncColumn="columnsInfo" @LoadMore="load_more" :key="'mycomments'"/>
    </div>
  </template>
  <script setup>
  import { computed, inject, onMounted, ref, watch } from 'vue'
  import { get_comments, delete_comment_by_id } from "@/api/community/main.js"
  import { noreapeat_array_byId} from"@/static/js/array/concat_two_norepeat"
  import { useRouter } from"vue-router"
  import LIST from "./list.vue"
  import { success } from '@/static/js/alert'
  const router=useRouter()
  const community = inject('community')()
  const user= inject('user')()
  const delete_comment = async (row) => { 
    const judge = await delete_comment_by_id(user.value.get_user()?.id, row.id)
    if (judge == 200) {
       // 删除成功
       success('删除成功')
       community.value.pop_my_comments_data_by_id(row.id)
    }
  }
  // 跳转文本位置
  const redirect_text_details = async (id) => {
    const to = router.resolve({ name: "community_render_text", query: { id: id } })
    window.open(to.href, '_blank')
  }
  
  const columnsInfo = [
        {
          label: '删除',
          value: 'delete',
          onclick: (row) => {
            delete_comment(row)
          }
    },
    {
      label: '定位',
      value: 'view',
      onclick: (row) => {
        redirect_text_details(row.id)
      }
    }
  ]
  
  const timer = ref(null)
  const searchText = ref('')     //实际是在分类的前提下进行处理,只不过从服务器获取刷新数据
  const mynavs = [
    {
        name: '层级',
        type: 'selection',
        column: 'isfloor',
        options: [
            { 'label': '全部', 'value': null },
            { 'label': '楼主', 'value': 'Landlord' },
            { 'label': '回复', 'value': 'replay' },
        ]
    },
    {
        name: '时间',
        type: 'selection',
        column:'date',
        options: [
            { 'label': '不限', 'value': null },
            { 'label': '最近一周', 'value': 'week' },
            { 'label': '最近一月', 'value': 'month' },
            { 'label': '最近一年', 'value': 'year' }
        ]
    }, 
    {
        name: 'search',
        type: 'input',
        placeholder: '搜索',
        options: []
    }
  ]
  const JSONSTRINGIFY = (data) => {
    let middleString=searchText.value
    Object.keys(data).forEach(key => {
        middleString += `${key}:${data[key]},`
    })
    return JSON.stringify(middleString)
  }
  const selectedValues = ref(
    mynavs
        .filter(item => item.type === 'selection')
        .reduce((acc, item) => {
            acc[item.column] = item.options[0].value;
            return acc;
        }, {})
  )

  const textdata =ref([])
  const middletextdata = computed(() => { 
    return community.value.get_my_comments_data_by_selection(JSONSTRINGIFY(selectedValues.value))
  })
  const load_more = async () => {
      //加载数据
      const column = JSONSTRINGIFY(selectedValues.value)
      const pre_meta = community.value.get_my_comments_meta(column)
      if (pre_meta != undefined) {
          
      } else { 
        if ((pre_meta?.limit || 1) * 20 >= pre_meta?.total_count) { 
          //该类已经没有可获得的新数据了
            success('已经没有更多数据了')
            return
        }
      }

      const judge =await get_comments(
        user.value.get_user()?.id,
        selectedValues.value,
        searchText.value,
        community.value.get_my_comments_meta(column)?.limit + 1 || 1,
    )
    if (judge != 0) { 
        const middledata = noreapeat_array_byId(
        community.value.get_my_comments_data_by_selection(column),
        judge.results,
          'id')
            community.value.set_my_comments_data(column, middledata)
            community.value.set_my_comments_meta(column, judge.meta)
      }
  }


  watch(() => middletextdata.value, (newVal) => {
      textdata.value = newVal
  }, {deep:true})
  
  
  watch(() => searchText.value, (newVal) => { 
    const history_data=community.value.get_my_comments_data_by_selection(JSONSTRINGIFY(newVal))
    if (history_data.length > 0) { 
      // 检测到缓存先下发缓存
      textdata.value = history_data
      return
    }
    if (timer.value) {
       clearTimeout(timer.value)
    }
    timer.value = setTimeout(async () => {
        load_more()
    }, 1500);
  })
    
  
  watch(() => selectedValues.value, (newVal) => {
    const history_data=community.value.get_my_comments_data_by_selection(JSONSTRINGIFY(newVal))
    if (history_data.length > 0) { 
      // 检测到缓存先下发缓存
      textdata.value = history_data
      return
    }
    if (timer.value) {
       clearTimeout(timer.value)
    }
    timer.value = setTimeout(async () => {
        load_more()
    }, 1500);
  }, {deep:true,})
  
  onMounted(async () => { 
      await load_more()   
  })
  </script>
  
  <style scoped>
  .mytext-layout {
    width: 100%;
    height: 100%;
    overflow: scroll;
  }
  .mytext-layout::-webkit-scrollbar {
    display: none;
  }
  
  .mytext-nav {
    width: 100%;
   
    min-height: 60px; 
    height: auto;     
    overflow: visible;
    padding: 10px 20px;
    background-color: #ffffff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
  }
  
  .nav-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 20px;
  }
  
  .left-items {
    display: flex;
    align-items: center;
    gap: 20px; 
    flex-wrap: wrap; 
    flex-grow: 1; 
    min-width: 0; 
  }
  
  .right-items {
    display: flex;
    align-items: center;
    flex-shrink: 0; 
  }
  
  .nav-item {
    display: flex;
    align-items: center;
    position: relative; 
  }
  
  .nav-label {
    font-size: 14px;
    color: #555;
    margin-right: 8px;
    white-space: nowrap;
  }
  
  .nav-select {
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
    color: #333;
    background-color: #fff;
    min-width: 120px;
    cursor: pointer;
    appearance: none; 
    background-repeat: no-repeat;
    background-position: right 8px center;
    background-size: 14px; 
    transition: border-color 0.3s;
  }
  
  .nav-select:focus {
    outline: none;
    border-color: #4a90e2;
    box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
  }
  
  .nav-select option { 
    background-color: white;
    color: #333;
  }
  .nav-select option:hover,
  .nav-select option:focus,
  .nav-select option:checked {
    background-color: #f0f0f0 ; 
    color: #333;
  }
  
  .search-container {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%; 
  }
  
  .nav-input {
    padding: 8px 12px 8px 35px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
    color: #333;
    width: 200px; 
    box-sizing: border-box; 
    transition: border-color 0.3s;
  }
  
  .nav-input:focus {
    outline: none;
    border-color: #4a90e2;
    box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
  }
  
  .search-icon {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #999;
    font-size: 16px; 
    pointer-events: none; 
  }
  
  /* 响应式设计 */
  @media (max-width: 768px) {
    .mytext-nav {
      padding: 10px; 
    }
    .nav-container {
        flex-wrap: wrap;
        align-items: center;
        gap: 15px; 
      
    }
    
    .left-items {
        gap: 15px; 
    }
    
    .right-items {
  
        flex-grow: 1; 
        min-width: 200px;
    }
    
    .nav-select {
        min-width: 100px; 
        flex-grow: 1; 
    }
    .nav-item { 
     
    }
    
    .nav-input {
        width: 100%; 
    }
  }
  
  @media (max-width: 480px) {
      .left-items, .right-items {
          width: 100%; 
          flex-grow: 0; 
          justify-content: flex-start; 
      }
      .left-items {
      
      }
      .left-items .nav-item {
         
      }
      .nav-select {
          width: 100%; 
      }
  }
  </style>